<?php
namespace Admin\Controller;
use Admin\Controller\CommonController;
class AdmsetController extends CommonController {
	protected $table = 'urlset';//后台导航设置表
	protected $tb_set = 'shop_setting';//系统设置表
	protected $tb_pay = 'payment' ;//支付方式表
	protected $tb_adv = 'adv';//广告表
	protected $tb_goods = 'product';//产品表
	protected $gc_class = 'goods_class';//商品分类表
	
    public function index(){
		
       $this->display();
    }	

	/*
	*后台导航设置
	*/	
    public function adminset(){
		
		$cache = 'setlist'.$_GET['p'];// 获取缓存数据 
		
		if (IS_POST) {
			$type = I('post.type');
			
			switch ($type) {
				case 'set_add':
					//增加
					I('post.set_stata')&& $result['set_stata'] = I('post.set_stata') ;
					I('post.set_name')? $result['set_name'] = I('post.set_name') :$this->error('必填项不能为空！');					
					I('post.set_url')? $result['set_url'] = I('post.set_url') :$this->error('必填项不能为空！');
					$result['set_power'] = I('post.set_power');
					$result['set_pid'] = I('post.set_pid');	
					
					$where_p ['set_id'] = $result['set_pid'];				
					$find_par = _getOne($this->table,$where_p);					
					$result['set_i'] = $find_par['set_i'];
					
					$where_o ['set_name'] = $result['set_name'];
					$find_one = _getOne($this->table,$where_o);
					if ($find_one) {
						$this->error('名称已存在！');
					}
					$stype = 1;
				break;
				case 'state':
					//开启
					$data['set_stata'] = I('post.state');
					$where['set_id'] = I('post.id');	
					$stype = 2;
				break;
				case 'edit':
					//获取数据
					$where['set_id'] = I('post.id');
					$stype = 3;
				break;
				case 'set_edit':
					//修改
					$data['set_name'] = I('post.set_name');
					//$data['set_url'] = I('post.set_url');
					$data['set_stata'] = I('post.set_stata');
					$where['set_id'] = I('post.id');
					$stype = 4;
				break;
				case 'del':
					//删除									
					$where['set_id'] = I('post.id');
					$str = "set_id=%d";
					$arr = I('post.id');
					$stype = 5;
				break;
			}
			if ($stype==1) {
				$add = _getAdd($this->table,$result,$cache);
				//dump($add);die;				
				if ($add) {
					unset($result);	
					$this->success('新增成功');		
				} else {
					$this->error('新增失败！');
				}					
			} else {
				$row = _getOne($this->table,$where);							
			} 

			if ($stype<>1 && $stype<>4) {
				//上级
				if ($row['set_pid']) {
					$parent = _getOne($this->table,array('set_id'=>$row['set_pid']));
				} else {
					$parent['set_name'] = '顶级导航';
				}
			}

			if ($row['set_power']<>'2') {
				if ($stype==2 || $stype==4) {
					$edit = _getEdit($this->table,$where,'',$data,$cache) ;
				}
				if ($stype==2) {
					//开启
					if ($edit) {
						$res =I('post.state');
					} else {
						$res=3;
					}					
					unset($where);
					unset($data);				
					
				} elseif ($stype==4) {					
					//修改
					unset($where);
					unset($data);						
					if ($edit) {
						$this->success('修改成功');	
					} else {
						$this->error('修改失败！');
					}						
						
				} elseif ($stype == 5 && $row['set_power']=='3') {
					//删除
					$del = _getDel($this->table,$str,$arr,$cache) ;
					if ($del) {
						$res =5;
					} else {
						$res=6;
					}
					unset($str);
					unset($arr);
					unset($where);	
				}
			} else {
				$res=4;
			}
			
			if ($stype) {
				//强制刷新
				_Refresh(1,$cache);
			}
			
			if ($stype<>1 && $stype<>4) {
				
				if ($stype==3) {					
					$this->ajaxReturn(array('edit'=>$res,'data'=>$row,'parent'=>$parent['set_name']));
				} else {
					unset($row);
					$this->ajaxReturn(array('info'=>$res));
				}				
			}

		} else {
			unset($data);
			unset($str);
			unset($order);
			$str = "set_stata>=%d and set_stata<=%d and set_power>=%d and set_power<=%d ";
			$data = array(1,2,1,3);
		
			$num = 15;
			$order = 'set_id asc';
			//分页
			$show = Page($this->table,$str,$data,$num);
			//强制刷新
			_Refresh($_GET['clear'],$cache);
			//列表
			$list = _getList($this->table,$str,$data,$cache,$order,$_GET['p'],$num);
			//dump($show);die;		
			$this->assign('page',$show);// 赋值分页输出			
			$this->assign('list',$list);
			$this->display();		
		}
    }
	
	
	/*
	*系统设置
	*
	*/
    public function set(){
		
		if (IS_POST) {
			$i = 0 ;
			foreach (I('post.') as $key => $v) {
				
			    $where ['name'] = $key ;					
				$data ['value'] = $v ;				
				$edit =  _getEdit($this->tb_set,$where,'',$data);
				if ($edit) {
					unset($data);
					unset($where);
					$i++;
				}				
			}
			if ($_FILES) {
				$where ['name'] = 'net_logo' ;
				$data ['value'] = upload() ;
				$edit =  _getEdit($this->tb_set,$where,'',$data);
				if ($edit) {
					unset($data);
					unset($where);
					$i++;
				}			
			}
			if ($i) {
				$cache = 'service_info' ;
				S($cache,null) ;
				$this->success('修改成功','set.html');
			} else {
				$this->error('修改失败','set.html');
			}
		} else {
			$str = "name<>'%s'";
			$data = array('');	
			//列表
			$list = _getList($this->tb_set,$str,$data);
			
			$this->assign('list',$list);			
			$this->display();      
		}
		 
    }

	/*
	*系统设置
	*
	*/
    public function clear(){
		if (IS_POST) {
			
			unset($data);
			if (I('post.all_cache')=='cache') {
				//清除所有S缓存
				I('post.shop_cache') && $data ['shop_cache'] = I('post.shop_cache') ;
				//清除所有文件
				I('post.shop_cache') && $data ['net_cache'] = I('post.net_cache') ;
				
				$data ['shop_cache'] && SClear() ;
				$data ['net_cache'] && NetClear() ;

				
				$data ? $this->success('清除缓存成功！','clear') :$this->error('清除缓存失败！','clear');
			}
		} else {
			//加载广告
			oncache('adv',$this->tb_adv);
			$this->display();
		}
	   
	}
	
	
	
	/*
	*支付方式列表
	*
	*/
    public function pay_set() { 	
		
		if (I('get.payment_id') || I('get.type')=='add') {
			$this->pay_add() ;
		} else {
			unset($arr);
			unset($str);
			unset($order);
			$str = "payment_id>%d";
			$arr = array(0);
			$order = 'payment_id asc';

			//列表
			$list = _getList($this->tb_pay,$str,$arr,'',$order);
			//dump($list);die;
			$this->assign('list',$list);
			$this->display();  			
		} 
	}	

	/*
	*支付方式增加、编辑页面
	*
	*/
    public function pay_add() { 	
		
		if (I('get.payment_id')) {
			
			unset($str);
			unset($arr);
			$str = "payment_id=%d";
			$arr = array(I('get.payment_id'));					
			//查询
			$pay = _getOne($this->tb_pay,$str,$arr);
			$pay['payment_config'] = unserialize($pay['payment_config']) ;
			//dump($pay);die;
			$this->assign('pay',$pay);
		}
		$this->assign('type',I('get.type')) ;
		$this->display('pay_add');	
	}
	
	/**
	*支付方式增加、编辑
	*/
	public function pay_add_on() {
		if (IS_POST) {
			if (I('post.pay_type')=="pay_more") {
				unset($str);
				unset($arr);
				unset($data);
				$str = "payment_id=%d" ;
				$arr = array(I('post.payment_id')) ;
				$data ['payment_state']= I('post.payment_state');
				I('post.payment_config') && $data ['payment_config']= serialize(I('post.payment_config'));
				//dump(I('post.'));die;
				$edit = _getEdit($this->tb_pay,$str,$arr,$data) ;
				
				if ($edit) {
					$this->success('编辑成功！','pay_set') ;
				} else {
					$this->error('编辑失败！','pay_set') ;
				}
				
			}
		}
	}
	
}